package com.wu.server.controller;


import com.wu.server.code.Code;
import com.wu.server.entity.Tag;
import com.wu.server.service.TagServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class TagController {
    @Autowired
    TagServer tagServer;
    @Autowired
    Code code;
    @GetMapping("tag")
    @ResponseBody
    public Object queryTag(
            @RequestParam(value = "id", defaultValue = "") String id,
            @RequestParam(value = "tagName", defaultValue = "") String tagName,
            @RequestParam(value = "summaryRange", defaultValue = "0,99999999") String summaryRange,
            @RequestParam(value = "order", defaultValue = "desc") String order,
            @RequestParam(value = "sort", defaultValue = "id") String sort,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        return tagServer.queryTag(id,tagName,summaryRange,order,sort,pageNum,pageSize);
    }
    @PostMapping("tag")
    @ResponseBody
    public Object addTag(@RequestBody TagForm form){
        Tag tag = new Tag();
        tag.tag_name = form.tagName;
        tag.tag_summary = form.summary;
        if(form.tagName == null || form.tagName.equals("")) return code.getMeta(Code.PARAM_ERROR,null);
        return tagServer.addTag(tag);
    }
    @PutMapping("tag")
    @ResponseBody
    public Object updateTag(@RequestBody TagForm form){
        if(form.id == 0 ||  form.tagName == null || form.tagName.equals("")) return code.getMeta(Code.PARAM_ERROR,null);
        Tag tag = new Tag();
        tag.id = form.id;
        tag.tag_name = form.tagName;
        tag.tag_summary = form.summary;
        return tagServer.updateTag(tag);
    }
    @DeleteMapping("tag")
    @ResponseBody
    public Object deleteTag(@RequestParam(value = "id") int id){
        if(id == 0) return code.getMeta(Code.PARAM_ERROR,null);
        return tagServer.deleteTag(id);
    }
    private static class TagForm{
        public int id;
        public String tagName;
        public int summary;
    }
}



